package com.hengtiansoft.lfy.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.google.zxing.WriterException;
import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.chart.utils.UserUtils;
import com.hengtiansoft.lfy.utils.Config;
import com.karics.library.zxing.encode.CodeCreator;

/**
 * @ClassName: CodeDialog
 * @Description: 二维码界面
 * @author caijiajun
 * @date 2015年12月29日 下午4:18:03
 * 
 */
public class CodeDialog extends Dialog {
    private Context context;
    private String username;
    private String nickname;
    private String sex;

    public CodeDialog(Context context, String username, String nickname,
                      String sex) {
        super(context);
        this.context = context;
        this.username = username;
        this.nickname = nickname;
        this.sex = sex;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_code, null);
        setContentView(view);

        TextView tvNickname = (TextView) view.findViewById(R.id.tv_nickname);
        TextView tvUsername = (TextView) view.findViewById(R.id.tv_username);
        ImageView mIvSex = (ImageView) view.findViewById(R.id.iv_sex);
        ImageView mIvCode = (ImageView) view.findViewById(R.id.iv_code);
        ImageView mIvAvatar = (ImageView) view
                .findViewById(R.id.iv_personal_img);
        tvNickname.setText(nickname);
        tvUsername.setText(username);
        if (sex != null) {
            if (sex.equals("m")) {
                mIvSex.setImageResource(R.drawable.me_sex);
            } else if (sex.equals("f")) {
                mIvSex.setImageResource(R.drawable.sex_girl);
            } else {
                mIvSex.setVisibility(View.GONE);
            }
        } else {
            mIvSex.setVisibility(View.GONE);
        }
        try {
            Bitmap qrCodeBitmap = CodeCreator.createQRCode(username);
            mIvCode.setImageBitmap(qrCodeBitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        if (Config.getUserName().equals(username)) {
            UserUtils.setCurrentUserAvatar(context, mIvAvatar);
        } else {
            UserUtils.setUserAvatar(context, username, mIvAvatar);
        }

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }
}
